package com.owen.cyclic.barrier.simple3;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class MyRunnable3 implements Runnable {

    private CyclicBarrier cyclicBarrier;
    private String name;
    private int sleep;

    public MyRunnable3(CyclicBarrier cyclicBarrier,String name,int sleep){
        this.cyclicBarrier=cyclicBarrier;
        this.name=name;
        this.sleep=sleep;
    }

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(sleep);
            long startTime=System.currentTimeMillis();
            cyclicBarrier.await();
            long endTime=System.currentTimeMillis();
            System.out.println(name+", sleep: "+sleep+" 等待了"+(endTime-startTime)+"(ms)，开始吃饭了！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
